<!--
 * @Descripttion: 报名情况
 * @Author: xiao li
 * @Date: 2020-10-15 10:24:27
 * @LastEditors: xiao li
 * @LastEditTime: 2021-11-19 18:39:47
-->
<template>
  <div>
    <top-nav :isBack="true"></top-nav>
    <div class="page-main">
      <div class="flex-warp mb-lg">
        <div v-for="(item, index) in tabList" :key="index">
          <lb-button
            size="medium"
            :type="item.status === searchForm.status ? 'primary' : ''"
            style="margin-right: 15px"
            @click="changeTabInd(item.status)"
            >{{ item.title }}</lb-button
          >
        </div>
      </div>

      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="用户昵称" prop="name">
            <el-input
              placeholder="请输入用户昵称"
              v-model="searchForm.name"
            ></el-input>
          </el-form-item>
          <el-form-item label="所属门店" prop="store_title">
            <el-input
              placeholder="请输入所属门店"
              v-model="searchForm.store_title"
            ></el-input>
          </el-form-item>
          <el-form-item label="报名时间" prop="range">
            <el-date-picker
              @change="toResetForm"
              v-model="searchForm.range"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="timestamp"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="page-search-form" style="padding-bottom: 24px">
        <el-row>
          <el-col :span="18" class="text-bold flex-y-center">数据列表</el-col>
          <el-col :span="6" class="text-right">
            <!-- <lb-button plain type='primary' icon='el-icon-download' :loading="downloadLoading" @click="toExportStaff">{{$t('action.export')}}</lb-button> -->
          </el-col>
        </el-row>
        <div class="space-lg"></div>
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="tableData"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
        >
          <!-- <el-table-column type="selection" width="55"></el-table-column> -->
          <el-table-column
            v-for="(item, index) in tableHeader"
            :key="index"
            :label="item.title"
            :width="
              item.type == 3
                ? '220'
                : item.icon === 'create_time'
                ? '110'
                : item.icon === 'store_name'
                ? '200'
                : ''
            "
          >
            <template slot-scope="scope">
              <div v-if="item.type == 3">
                <div class="flex-warp">
                  <div
                    :class="[{ 'ml-md mr-md': aindex == 1 }]"
                    v-for="(aitem, aindex) in scope.row[item.icon]"
                    :key="aindex"
                  >
                    <lb-image :src="aitem"></lb-image>
                  </div>
                </div>
              </div>
              <div v-else>
                <!-- 头像 -->
                <block v-if="item.icon === 'avatarUrl'">
                  <lb-image :src="scope.row[item.icon]"></lb-image>
                </block>
                <!-- 状态 -->
                <block v-else-if="item.icon === 'status'">
                  {{ statusType[scope.row[item.icon]] }}
                </block>
                <!-- 创建时间 -->
                <block v-else-if="item.icon === 'create_time'">
                  <p>{{ scope.row[item.icon] | handleTime(1) }}</p>
                  <p>{{ scope.row[item.icon] | handleTime(2) }}</p>
                </block>
                <block v-else>
                  {{ scope.row[item.icon] }}
                </block>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="操作">
            <template slot-scope="scope">
              <div class="table-operate">
                <lb-button
                  type="primary"
                  size="mini"
                  plain
                  @click="toShowDialog(scope.row, 'view')"
                  v-if="scope.row.status !== 1"
                  >{{ $t('action.view') }}</lb-button
                >
                <lb-button
                  type="danger"
                  size="mini"
                  plain
                  @click="toShowDialog(scope.row, 'edit')"
                  v-if="scope.row.status === 2"
                  >审核</lb-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- :isShowBatch="false" -->
        <lb-page
          :batch="false"
          :page="searchForm.page"
          :pageSize="searchForm.limit"
          :total="total"
          :selected="multipleSelection.length"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
        >
        </lb-page>

        <el-dialog
          title="报名详情"
          :visible.sync="showDialog"
          width="800px"
          center
        >
          <div class="dialog-inner">
            <el-form
              @submit.native.prevent
              :model="dialogForm"
              ref="dialogForm"
              label-width="85px"
              class="basic-form"
            >
              <div class="flex-center">
                <lb-image class="avatar" :src="dialogForm.avatarUrl"></lb-image>
                <div class="flex-1 ml-lg f-paragraph">
                  <div>昵称：{{ dialogForm.nickName }}</div>
                  <div class="mt-sm">
                    报名时间：{{ dialogForm.create_time | handleTime }}
                  </div>
                </div>
              </div>
              <div class="ml-lg space-md"></div>
              <div class="ml-lg space-md b-1px-b"></div>
              <div class="mg-lg flex-warp">
                <div
                  v-for="(item, index) in tableHeader"
                  :key="index"
                  :style="{ width: isHaveArr.includes(item.icon) ? '' : '50%' }"
                >
                  <block v-if="isHaveArr.includes(item.icon)"></block>
                  <div class="flex-warp pt-lg mt-lg" v-else>
                    <div>{{ item.title }}</div>
                    <div class="flex-1 ml-lg">
                      <div v-if="item.type == 3">
                        <lb-cover
                          :fileList="dialogForm[item.icon]"
                          :isToDel="false"
                          fileType="image"
                          type="more"
                          size="small"
                          :fileSize="dialogForm[item.icon].length"
                          v-if="showDialog"
                        ></lb-cover>
                        <!-- <div class="flex-warp">
                    <div :class="[{'ml-md mr-md':aindex==1}]" v-for="(aitem,aindex) in dialogForm[item.icon]" :key="aindex">
                      <lb-image class="avatar" :src="aitem"></lb-image>
                    </div>
                  </div> -->
                      </div>
                      <div v-else>
                        <!-- 状态 -->
                        <block v-if="item.icon === 'status'">
                          {{ statusType[dialogForm[item.icon]] }}
                        </block>
                        <block v-else>
                          {{ dialogForm[item.icon] }}
                        </block>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="ml-lg space-md"></div>
              <div class="ml-lg space-md b-1px-b"></div>
              <div class="ml-lg space-md"></div>
              <el-form-item label="所属门店">
                <div>{{ dialogForm.store_name }}</div>
              </el-form-item>
              <el-form-item label="审核状态">
                <div
                  :class="[
                    { 'c-warning': dialogForm.status !== 3 },
                    { 'c-link': dialogForm.status === 3 }
                  ]"
                >
                  {{ statusType[dialogForm.status]
                  }}<block v-if="dialogForm.status == -1 && dialogForm.text"
                    >，拒绝原因：{{ dialogForm.text }}</block
                  >
                </div>
              </el-form-item>
              <el-form-item
                label="审核结果"
                v-if="dialogForm.clickType == 'edit'"
              >
                <el-radio-group v-model="dialogForm.isPass">
                  <el-radio :label="1">同意</el-radio>
                  <el-radio :label="0">拒绝</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                label="审核意见"
                v-if="dialogForm.clickType == 'edit'"
              >
                <el-input
                  type="textarea"
                  :rows="4"
                  resize="none"
                  maxlength="100"
                  placeholder="请输入审核不通过原因，以便用户再次提交"
                  show-word-limit
                  v-model="dialogForm.text"
                  style="width: 600px"
                >
                </el-input>
              </el-form-item>
            </el-form>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="toShowDialog">取 消</el-button>
            <el-button
              type="primary"
              @click="submitDialogForm"
              v-if="dialogForm.clickType == 'edit'"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import pubic from '@/utils/public'
export default {
  data () {
    return {
      isHaveArr: ['nickName', 'avatarUrl', 'store_name', 'create_time', 'status'],
      statusType: {
        '-1': '审核不通过',
        1: '预报名',
        2: '待审核',
        3: '审核通过'
      },
      tabList: [
        {
          title: '全部',
          text: '全部',
          key: 'all_count',
          status: 0
        },
        // {
        //   title: '未提交',
        //   status: 1
        // },
        {
          title: '待审核',
          text: '待审核',
          key: 'be_sh_count',
          status: 2
        },
        {
          title: '审核通过',
          text: '审核通过',
          key: 'pass_count',
          status: 3
        },
        {
          title: '审核驳回',
          text: '审核驳回',
          key: 'no_pass_count',
          status: -1
        }
      ],
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        }
      },
      searchForm: {
        status: 0,
        name: '',
        store_title: '',
        range: '',
        atv_id: 0,
        page: 1,
        limit: 10
      },
      downloadLoading: false,
      multipleSelection: [],
      loading: false,
      tableHeader: [],
      tableData: [],
      total: 0,
      showDialog: false,
      dialogForm: {}
    }
  },
  created () {
    let { id } = this.$route.query
    this.searchForm.atv_id = id
    this.getTableHeader()
    this.getTableDataList()
  },
  methods: {
    changeTabInd (status) {
      this.searchForm.status = status
      this.handleCurrentChange(1)
    },
    toResetForm () {
      this.$refs.multipleTable.clearSelection()
    },
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    async getTableHeader () {
      let { code, data } = await this.$api.overlordAtvRecordTitle({ atv_id: this.searchForm.atv_id })
      if (code !== 200) return
      this.tableHeader = data
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { searchForm } = this
      let isChange = typeof (searchForm.range) === 'object' && searchForm.range && searchForm.range.length > 0
      searchForm.start_time = isChange ? searchForm.range[0] / 1000 : ''
      searchForm.end_time = isChange ? searchForm.range[1] / 1000 + 60 * 60 * 24 - 1 : ''
      this.$api.overlordAtvRecordList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          let data = res.data.data
          data.map(item => {
            item.check = false
          })
          this.tableData = data
          this.total = res.data.total
          this.tabList.map(item => {
            item.title = `${item.text}（${res.data[item.key]}）`
          })
        }
      })
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
       * @method 导出
       */
    toExportStaff () {
      this.downloadLoading = true
      let { id: activityId, range, status } = JSON.parse(JSON.stringify(this.searchForm))
      let multipleSelection = JSON.parse(JSON.stringify(this.multipleSelection))
      let searchForm = {
        atv_id: activityId,
        id: [],
        start_time: '',
        end_time: '',
        status
      }
      if (typeof (range) === 'object' && range && range.length > 0) {
        searchForm.start_time = range[0] / 1000
        searchForm.end_time = range[1] / 1000 + 24 * 3600 - 1
      }
      if (typeof (multipleSelection) === 'object' && multipleSelection && multipleSelection.length > 0) {
        multipleSelection.map(item => {
          searchForm.id.push(item.id)
        })
      }
      let url = pubic.getProCurrentHref()
      let keywords = url.indexOf('?') > 0 ? '' : '?'
      let flag = url.indexOf('?') > 0
      Object.getOwnPropertyNames(searchForm).forEach((keys, value) => {
        keywords += flag ? `&${keys}=${searchForm[keys]}` : `${keys}=${searchForm[keys]}`
        flag = true
      })
      let token = sessionStorage.getItem('minitk')
      let dwonlaodUrl = `${url}/activity/admin/activityUserRecordAdmin/userRecordExcel${keywords}&token=${token}`
      window.location.href = dwonlaodUrl
      setTimeout(() => {
        this.downloadLoading = false
      }, 5000)
    },
    toShowDialog (item = {}, type = '') {
      let arr = JSON.parse(JSON.stringify(item))
      if (arr.id) {
        this.tableHeader.map(aitem => {
          if (aitem.type === 3) {
            arr[aitem.icon] = arr[aitem.icon].map(bitem => {
              return { url: bitem }
            })
          }
        })
      }
      arr.clickType = type
      arr.isPass = 1
      arr.text = arr.status === -1 && arr.text ? arr.text : ''
      this.dialogForm = arr
      this.showDialog = !this.showDialog
    },
    async submitDialogForm () {
      let { id, isPass, text = '' } = this.dialogForm
      let methodModel = isPass ? 'overlordAtvPassRecord' : 'overlordAtvNoPassRecord'
      let { code } = await this.$api[methodModel]({ id, text })
      if (code !== 200) return
      this.$message.success(this.$t('tips.successSub'))
      this.toShowDialog()
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
